package ly.gupao.edu.java8.Topic11;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static java.util.stream.Collectors.toList;

/**
 * @author: liyang
 * @date：2018/11/15 0015
 * describe：
 */
public class Discount {

    private Executor executor =
            Executors.newFixedThreadPool(20,
                r -> {
                    Thread thread = new Thread(r);
                    thread.setDaemon(true);
                    return thread;
                    });

    public static void main(String[] args) {
        Long start = System.nanoTime();
        Discount discount = new Discount();
        System.out.println(discount.findPrices("xxx"));
        System.out.println("-------" + (System.nanoTime() - start) / 1_000_000 + "--------");
    }

    public List<String> findPrices(String product) {
        System.out.println("list--------------------");
        List<String> list = shops.stream()
                .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrise(product), executor))
                .map(future -> future.thenAccept(Quote::parse))
                .map(future -> future.thenCompose(quote ->
                        CompletableFuture.supplyAsync(() -> Quote.applyDiscount(quote), executor)
                ))
                .map(CompletableFuture::join)
                .collect(toList());
        System.out.println("list--------------------");
        return list;
    }

    List<Shop> shops = Arrays.asList(new Shop("BestPrice"),
            new Shop("LetsSaveBig"),
            new Shop("MyFavoriteShop"),
            new Shop("MyFavoriteShop1"),
            new Shop("BuyItAll"));

    static class Quote{

        private final String shopName;
        private final double price;
        private final Discount.Code discountCode;

        public static String applyDiscount(Void quote) {
            return quote + " price is ";
        }

        public Quote(String shopName, double price, Code discountCode) {
            this.shopName = shopName;
            this.price = price;
            this.discountCode = discountCode;
        }

        public static Quote parse(double s) {
            return new Quote("xxx", 0.4,Code.GOLD );
        }

        public String getShopName() { return shopName; }
        public double getPrice() { return price; }
        public Discount.Code getDiscountCode() { return discountCode; }
    }


    /**
     * 以枚举类型定义的折扣代码
     */
    enum Code{
        NONE(0), SILVER(5), GOLD(10), PLATINUM(15), DIAMOND(20);
        private final int percentage;
        Code(int percentage) {
            this.percentage = percentage;
        }
    }


    class Shop{

        private String name;
        private double price;

        public Shop(String name) {
            this.name = name;
        }

        public String getName(){
            return this.name;
        }

        public double getPrise(String product){
            return calculate(product);
        }

        public double calculate(String product){
            delay();
            Random random = new Random();
            return random.nextDouble() * product.charAt(0) + product.charAt(1);
        }

        public void delay(){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
